import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-08-04
 * Time: 15:55
 */
class Solution5 {
    public int minNumberOfFrogs(String croakOfFrogs) {
        char[] ch = croakOfFrogs.toCharArray();
        String t = "croak";
        int n = t.length();
        int[] hash = new int[n];

        // 下标与字符的对应关系
        Map<Character,Integer> index = new HashMap<>();

        for(int i = 0; i < n; i++) {
            index.put(t.charAt(i),i);
        }

        for(int i = 0; i < ch.length; i++) {
            if(ch[i] == t.charAt(0)) {
                if(hash[n-1] != 0) {
                    hash[n-1]--;
                }
                hash[0]++;
            }else {
                int j = index.get(ch[i]);
                if(hash[j-1] == 0) {
                    return -1;
                }
                hash[j]++;
                hash[j-1]--;
            }
        }

        // 再来判定一遍
        for(int i = 0; i < n-1; i++) {
            if(hash[i] != 0) {
                return -1;
            }
        }
        return hash[n-1];
    }
}
